/*
 * Created by JFormDesigner on Wed Jun 18 16:25:10 CST 2025
 */
package MEET.src.View;

import MEET.src.code.UserDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author lenovo
 */
public class Login extends JFrame {
    public Login() {
        initComponents();
    }


    public void button1(ActionEvent e) {
        String username = textField1.getText();
        String password = new String(textField2.getText());
        if (UserDao.checkLogin(username, password)) {
            // 登录成功，弹出成功提示
            JOptionPane.showMessageDialog(this, "登录成功！");

            // 创建主界面实例并设置其可见
            main mainForm = new main(username);
            mainForm.setVisible(true);
            // 关闭登录界面
            this.dispose();
        } else {
            // 登录失败，弹出失败提示
            JOptionPane.showMessageDialog(this, "用户名或密码错误，请重试！", "登录失败", JOptionPane.ERROR_MESSAGE);
        }
    }

    //


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        setFont(new Font("Footlight MT Light", Font.BOLD, 47));
        setTitle("\u4e0a\u6d77\u7535\u529b\u5927\u5b66\u4f1a\u8bae\u9884\u8ba2\u7cfb\u7edf");
        setResizable(false);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("    \u4f1a \u8bae \u9884 \u8ba2 \u7cfb \u7edf");
        label1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 36));
        contentPane.add(label1);
        label1.setBounds(115, 35, 365, 65);

        //---- label2 ----
        label2.setText("\u8d26\u53f7\uff1a");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        contentPane.add(label2);
        label2.setBounds(120, 135, 60, 35);

        //---- label3 ----
        label3.setText("\u5bc6\u7801\uff1a");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        contentPane.add(label3);
        label3.setBounds(120, 180, 65, 40);
        contentPane.add(textField1);
        textField1.setBounds(200, 135, 255, 35);
        contentPane.add(textField2);
        textField2.setBounds(200, 185, 255, 35);

        //---- button1 ----
        button1.setText("\u767b\u5f55");
        contentPane.add(button1);
        button1.setBounds(100, 255, 140, 50);
        button1.addActionListener(this::button1);

        //---- button2 ----
        button2.setText("\u6ce8\u518c");
        contentPane.add(button2);
        button2.setBounds(305, 255, 140, 50);
        button2.addActionListener(e -> new Register().setVisible(true));
        button2.addActionListener(e -> this.dispose());

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(585, 415);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
//
    public static void main(String[] args) {
        UserDao.testConnection();
        SwingUtilities.invokeLater(() -> {
            Login loginForm = new Login();
            loginForm.setVisible(true);
        });

    }
}
